package net.hn.hnms.biz.meeting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.meeting.domain.MeetingRoom;
import net.hn.hnms.biz.meeting.mapper.MeetingRoomMapper;
import net.hn.hnms.biz.meeting.request.MeetingRoomReq;
import net.hn.hnms.biz.meeting.service.MeetingRoomService;
import net.hn.hnms.biz.meeting.vo.MeetingRoomVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;



@Service
public class MeetingRoomServiceImpl  extends BaseService<MeetingRoom, MeetingRoomVo> implements  MeetingRoomService {

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;


    private LambdaQueryWrapper<MeetingRoom> buildQuery(MeetingRoomReq req){
        return new LambdaQueryWrapper<MeetingRoom>()
                .and(o->o.eq(req.getMineCode()!=null, MeetingRoom::getMineCode, req.getMineCode()).or().eq(req.getCompanyCode()!=null, MeetingRoom::getCompanyCode, req.getCompanyCode()))
                .like(req.getMeetingRoomAddress() != null && req.getMeetingRoomAddress()!="", MeetingRoom::getMeetingRoomAddress,req.getMeetingRoomAddress())
                .like(req.getMeetingRoomName() != null, MeetingRoom::getMeetingRoomName,req.getMeetingRoomName())
                .eq(StrUtil.isNotBlank(req.getMeetingRoomType()), MeetingRoom::getMeetingRoomType, req.getMeetingRoomType());
    }

    @Override
    public ResponsePage<MeetingRoomVo> selectPageList(MeetingRoomReq req, PageQuery pageQuery) {

        Page<MeetingRoom> page = meetingRoomMapper.selectPage(pageQuery.build(), buildQuery(req).orderByDesc(MeetingRoom::getCreateTime));
        List<MeetingRoomVo> list = page.getRecords().stream().map(o -> convert(o,MeetingRoomVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(list, page.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDeleteByIds(List<String> idList) {
        meetingRoomMapper.updateDeleteByIds(idList);
    }

    @Override
    public int insert(MeetingRoomReq meetingRoomReq) {
        MeetingRoom insertObj = new MeetingRoom();
        BeanUtil.copyProperties(meetingRoomReq, insertObj);
        return meetingRoomMapper.insert(insertObj);
    }

    @Override
    public void updateById(MeetingRoomReq meetingRoomReq) {
        MeetingRoom updateObj = new MeetingRoom();
        BeanUtil.copyProperties(meetingRoomReq, updateObj);
        int updateCount = meetingRoomMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public List<MeetingRoomVo> selectReservePageList(Date dateTime,String mineCode,String companyCode) {
        List<MeetingRoomVo> list = meetingRoomMapper.selectReservePageList(dateTime,mineCode,companyCode);
        return list;
    }
    /**
     * 启用/禁用
     * @param id 数据id
     * @param status  status: 1:启用 0:禁用
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeStatus(String meetingRoomId, Integer state) {
            if (state == 1 || state == 0) {
                MeetingRoom meetingRoom = meetingRoomMapper.selectById(meetingRoomId);
                if (meetingRoom == null) {
                    throw new HuaNengException(Constants.FAIL, "数据不存在");
                }
                meetingRoom.setState(state);
                int res = meetingRoomMapper.updateById(meetingRoom);
                if (res == 0) {
                    throw new HuaNengException(Constants.FAIL, "数据不存在");
                }
                return;
            }
            throw new HuaNengException(Constants.FAIL, "传入的参数错误");

    }


}
